/*
 * @(#)MultipleDocumentSelectionTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.taglib;

import java.util.StringTokenizer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

import com.dcivision.framework.taglib.html.FormTag;
import com.dcivision.framework.taglib.html.TextTag;
import com.dcivision.framework.web.WebUtil;
/**
    MultipleDocumentSelectionTag.java

    <p>This class is to extend the TextTag provided by Apache Struts.<p>
    <p>Modification in "struts-html.tld"<br>

  <pre>
     [ADDED]
      &lt;tagclass&gt;com.dcivision.dms.taglib.MultipleDocumentSelectionTag&lt;/tagclass&gt;
      &lt;attribute&gt;
      &lt;name&gt;rootID&lt;/name&gt;
      &lt;required&gt;true&lt;/required&gt;
      &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
      &lt;/attribute&gt;
      &lt;attribute&gt;
      &lt;name&gt;parentID&lt;/name&gt;
      &lt;required&gt;true&lt;/required&gt;
      &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
      &lt;/attribute&gt;
  </pre>
    </p>

  <p>USAGE:</p>
  <p>The following example shows the MultipleDocumentSelection usage. </p>
  <p><code>&lt;dms:multipleDocumentSelection property='fileList' <strong>rootID='&lt;%=rootID%&gt;' parentID='&lt;%=parentID%gt;'</strong>/&gt;</code></p>

  @author Angus Shiu
  @company DCIVision Limited
  @creation date   17/11/2003
  @version $Revision: 1.5.30.3 $
 */
public class MultipleDocumentSelectionTag extends TextTag {

  public static final String REVISION = "$Revision: 1.5.30.3 $";

  protected Log log = LogFactory.getLog(this.getClass().getName());

  /** Holds value of property rootID. */
  private String rootID = null;

  /** Holds value of property parentID. */
  private String parentID = null;

  public MultipleDocumentSelectionTag() {
    super();
    this.setReadonly(true);
  }

  /**
   * release
   */
  public void release() {
    super.release();
    this.setReadonly(true);
  }

  /** Getter for property rootID.
   * @return Value of property rootID.
   */
  public String getRootID() {
    return this.rootID;
  }

  /** Setter for property rootID.
   * @param rootID New value of property rootID.
   */
  public void setRootID(String rootID) {
    this.rootID = rootID;
  }

  /** Getter for property parentID.
   * @return Value of property parentID.
   */
  public String getParentID() {
    return this.parentID;
  }

  /** Setter for property parentID.
   * @param parentID New value of property parentID.
   */
  public void setParentID(String parentID) {
    this.parentID = parentID;
  }

  /** overriden parent's doStartTag
   * @return
   * @throws JspException
   */
  public int doStartTag() throws javax.servlet.jsp.JspException {

    int retValue = SKIP_BODY;
    JspWriter out = pageContext.getOut();
    FormTag formTag = (FormTag)BodyTagSupport.findAncestorWithClass(this, FormTag.class);

    try {
      String sPropertyName = this.getProperty();
      Object value;
      String sDisplayStr="";
      String sHiddenStr="";
      value = RequestUtils.lookup(pageContext, name, property, null);

      if (value==null) {
        value = "";
        sHiddenStr = "";
      } else {
        //log.debug("value="+value);
        sHiddenStr = ResponseUtils.filter(value.toString());
        StringTokenizer selectedList = new StringTokenizer(sHiddenStr, ",");
        while( selectedList.hasMoreTokens()) {
          String selectedDoc = selectedList.nextToken();

          if (selectedDoc!=null) {
            int delimLoc = selectedDoc.indexOf("|");
            if (delimLoc!=-1) {
              sDisplayStr += selectedDoc.substring(delimLoc+1) + ";";
            }
          }
        }

      }


      // display value
      this.setProperty("display_"+sPropertyName);
      this.setValue(sDisplayStr);
      retValue = super.doStartTag();

      // hidden value, which stores the document id (docID), the version id (versionID) and the display file name (displayName)
      // in the format {docID;versionID|displayName,docID;versionID|displayName,......}
      this.setProperty(sPropertyName);
      this.setValue(sHiddenStr);


      log.debug("image!!!!!!!!!");
      // show the selection panel if it is for editing
      //if (!textOnly && !formTag.getTextOnly()) {
        out.print("<a href=\"javascript://\" ");
        out.print("onclick=\"return popupDocumentSelectionPanel("+rootID+", "+parentID+", 'display_"+sPropertyName+"', '"+sPropertyName+"');\">");

        out.print("<img ");
        out.print("src=\""+WebUtil.getImage(pageContext.getRequest(), "IconDmsSelectPanel.gif")+"\" ");
        out.print("name=\"imgCalendar_"+property+"\" ");
        out.print("align=\"absmiddle\" ");
        out.print("border=\"0\"");
        out.print("alt=\"\"");
        out.print(">");

        out.println("</a>");
      //}


      log.debug("hidden!!!!!!!!!");
      out.print("<input type=\"hidden\" ");
      out.print("name=\""+sPropertyName+"\" ");
      out.println("value=\""+sHiddenStr+"\">");

      this.setValue(null);
      return retValue;
    }catch(Exception e) {
      log.error(e, e);
      throw new JspException(e);
    }
  }
}